package com.rgs.platform.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageInfo;
import com.rgs.base.core.model.WebApiReturnResultModel;
import com.rgs.base.core.page.IPageRequestModel;
import com.rgs.base.core.page.IPageResponseModel;
import com.rgs.base.database.DataPageUtil;
import com.rgs.model.platform.vo.CovidInfoVO;
import com.rgs.platform.dto.covid.ListCovidPageRequestDTO;
import com.rgs.platform.entity.CrawlingCovidDO;
import com.rgs.platform.filter.CovidInfoFilterDTO;
import com.rgs.platform.service.CrawlingCovidService;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Ren Gaoshuai
 * @ClassName CovidInfoController
 * @date 2022-01-24 11:27
 **/
@RestController
@RequestMapping("/v1/covid")
public class CovidInfoController {

    @Resource
    CrawlingCovidService covidInfoService;

    @ApiOperation("/获取疫情数据详情")
    @GetMapping("/{id}")
    public WebApiReturnResultModel<CovidInfoVO> selectByCovidId(@PathVariable("id") String id) {
        CovidInfoVO covidInfoVO = BeanUtil.copyProperties(covidInfoService.selectById(id), CovidInfoVO.class);
        return WebApiReturnResultModel.ofSuccess(covidInfoVO);
    }

    @ApiOperation("分页获取疫情数据")
    @PostMapping("/page")
    public WebApiReturnResultModel<IPageResponseModel<CovidInfoVO>> page(
            @RequestBody @Validated ListCovidPageRequestDTO listCovidPageRequest) {
        IPageResponseModel<CovidInfoVO> iPageResponseModel = commonCovidPage(listCovidPageRequest, listCovidPageRequest);
        return WebApiReturnResultModel.ofSuccess(iPageResponseModel);
    }

    private IPageResponseModel<CovidInfoVO> commonCovidPage(IPageRequestModel iPageRequestModel,
                                                            ListCovidPageRequestDTO listCovidPageRequest) {
        //获取数据
        PageInfo<CrawlingCovidDO> pageInfo = obtainPageList(iPageRequestModel, listCovidPageRequest);

        //DO转VO
        List<CovidInfoVO> voList = pageInfo.getList().stream().map(q ->
                BeanUtil.copyProperties(q, CovidInfoVO.class)).collect(Collectors.toList());

        return DataPageUtil.buildResponseModel(pageInfo, voList);
    }

    private PageInfo<CrawlingCovidDO> obtainPageList(IPageRequestModel iPageRequestModel,
                                                     ListCovidPageRequestDTO listCovidPageRequest) {
        return covidInfoService.listPageByFilter(
                iPageRequestModel,
                CovidInfoFilterDTO.builder()
                        .pid(listCovidPageRequest.getPid())
                        .locationId(listCovidPageRequest.getLocationId())
                        .provinceName(listCovidPageRequest.getProvinceName())
                        .provinceShortName(listCovidPageRequest.getProvinceShortName())
                        .cityName(listCovidPageRequest.getCityName())
                        .dateTimeStr(listCovidPageRequest.getDateTimeStr())
                        .cityType(listCovidPageRequest.getCityType())
                        .build()
        );
    }

    @ApiOperation("疫情数据导出")
    @PostMapping("/export")
    public WebApiReturnResultModel<Void> export(
            @RequestBody @Validated ListCovidPageRequestDTO listCovidPageRequest) {
        return WebApiReturnResultModel.ofSuccess();
    }

    /**
     * 获取省级数据(分页)
     */
    @ApiOperation("分页获取疫情数据")
    @PostMapping("/pageProvice")
    public WebApiReturnResultModel<IPageResponseModel<CovidInfoVO>> pageByProvice(
            @RequestBody @Validated ListCovidPageRequestDTO listCovidPageRequest) {
        if (StrUtil.isEmpty(listCovidPageRequest.getDateTimeStr())) {
            //日期为空，那就迷人传当前的日期
            listCovidPageRequest.setDateTimeStr(DateUtil.formatDate(new Date()));
        }

        listCovidPageRequest.setCityType(2);
        IPageResponseModel<CovidInfoVO> iPageResponseModel = commonCovidPage(listCovidPageRequest, listCovidPageRequest);
        return WebApiReturnResultModel.ofSuccess(iPageResponseModel);
    }

    /**
     * 获取市级数据（不分页）
     */
    @ApiOperation("获取市级疫情数据")
    @PostMapping("/city")
    public WebApiReturnResultModel<List<CovidInfoVO>> selectCityByProvice(
            @RequestBody @Validated ListCovidPageRequestDTO listCovidPageRequest) {
        if (StrUtil.isEmpty(listCovidPageRequest.getDateTimeStr())) {
            //日期为空，那就迷人传当前的日期
            listCovidPageRequest.setDateTimeStr(DateUtil.formatDate(new Date()));
        }
        listCovidPageRequest.setCityType(1);
        List<CrawlingCovidDO> covidDOList = covidInfoService.listByFilter(
                CovidInfoFilterDTO.builder()
                        .pid(listCovidPageRequest.getLocationId())
                        .dateTimeStr(listCovidPageRequest.getDateTimeStr())
                        .cityType(listCovidPageRequest.getCityType())
                        .orderByClause(listCovidPageRequest.getOrderBy())
                        .build());
        List<CovidInfoVO> voList = covidDOList.stream().map(q -> BeanUtil.copyProperties(q, CovidInfoVO.class)).collect(Collectors.toList());
        return WebApiReturnResultModel.ofSuccess(voList);
    }
}
